package com.jetlion.contentresovler;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initListen();
    }

    private void initListen() {
        Button addBtn = findViewById(R.id.add_btn);
        Button queryBtn = findViewById(R.id.query_btn);
        addBtn.setOnClickListener(this);
        queryBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.add_btn:
                add();
                break;
            case R.id.query_btn:
                query();
                break;
            default:
                break;
        }
    }

    private void add() {
        ContentValues values = new ContentValues();

        //插入一条数据，获得新的id
        Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);

        //设置插入单条数据
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);

        //设置id
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

        //设置名称
        values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "zhangsan");

        //插入联系人数据
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);


        //设置插入单条数据
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

        //设置id
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

        //设置电话号码
        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "15505962668");

        //设置电话号码类型
        values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

        //插入电话号码数据
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    }


    @SuppressLint("Range")
    private void query() {

        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            while (phoneCursor.moveToNext()) {
                String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.i("MainActivity", "name: " + name + ",phone:" + phone);
            }
        }
    }
}